﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_FriendLink")]
    public class T_FriendLink : IEntity<T_FriendLink>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public int RelationId { get; set; }
        /// <summary>
        /// 1:期刊 2:会议
        /// </summary>
        public int RelationType { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Title { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string URL { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Remark { get; set; }
        public DateTime CreateTime { get; set; }

        public override T_FriendLink Find(string key)
        {
            int id = key.ToInt32();
            return Find(c => c.Id == id);
        }

        public override T_FriendLink Find(System.Linq.Expressions.Expression<Func<T_FriendLink, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.FriendLink.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.RelationId = item.RelationId;
                    this.RelationType = item.RelationType;
                    this.Title = item.Title;
                    this.URL = item.URL;
                    this.Remark = item.Remark;
                    this.CreateTime = item.CreateTime;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_FriendLink, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.FriendLink.Any(where);
            }
        }
    }
}
